import Vue from "vue";
import VueRouter from "vue-router";
Vue.use(VueRouter);
import Login from "../views/Login.vue";
import Reigster from "../views/Reigster.vue";
import User from "../views/User.vue";
import Usercompile from "../views/Usercompile.vue";
import Home from "../views/Home.vue";
import Aritcle from "../views/Aritcle.vue";
import UserFollow from "../views/user_follow.vue";
import UserStar from "../views/UserStar.vue";
import Usercomments from "../views/Usercomments.vue";
import Comment from "../views/Comment.vue";
import Category from "../views/Category.vue";
import Search from "../views/Search.vue";
import { getToken } from "../utils/tokenData";
const routes = [
  { path: "", redirect: "/home", },
  { path: "/home", component: Home},
  { path: "/login", component: Login, name: "login" },
  { path: "/reigster", component: Reigster },
  { path: "/user", component: User },
  { path: "/usercompile", component: Usercompile },
  { path: "/aritcle/:id", component: Aritcle },
  { path: "/user_follow", component: UserFollow },
  { path: "/user_star", component: UserStar },
  { path: "/user_comments", component: Usercomments },
  { path: "/category", component: Category  },
  { path: "/search", component: Search  },
  { path: "/comment/:id", component: Comment },
];
const router = new VueRouter({
  mode: "history",
  routes,
});
router.beforeEach((to, from, next) => {
  const pathArr = ["/user"];
  if (pathArr.indexOf(to.path) != -1) {
    if (getToken()) {
      next();
    } else {
      next({ path: "/login" });
    }
  } else {
    next();
  }
});
export default router;
